/*
 * Copyright 2010-2011 WebEqua Team
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package org.mathcloud.math.core.matlab.quantity;

//import classes slf4j
import org.mathcloud.math.core.matlab.quantity.MatlabQuantityObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

//import classes org.webequa.core
import org.webequa.core.common.quantity.RealNumber;
import org.webequa.core.generic.signature.GenericQuantityObjectSignature;

//import classes com.mathworks.toolbox.javabuilder
import com.mathworks.toolbox.javabuilder.MWClassID;
import com.mathworks.toolbox.javabuilder.MWNumericArray;


public class MatlabRealNumber extends MatlabQuantityObject implements RealNumber{
	public MatlabRealNumber()
	{
		super();
		try {
			mtlbObject =  new MWNumericArray(0, MWClassID.DOUBLE);
			log.info("{}={}",mtlbVarName,mtlbObject.toString());
		}
		catch(Exception e) {
			log.error("Exception:",e);
		}
		
		signature = GenericQuantityObjectSignature.getRealNumberSignature();
		signature.setRealizationName("matlab");
	}
	
	public MatlabRealNumber(double in)
	{
		super();
		try {
			mtlbObject = new MWNumericArray(in, MWClassID.DOUBLE);
			log.info("{}={}",mtlbVarName,mtlbObject.toString());
		}
		catch(Exception e) {
			log.error("Exception:",e);
		}
		signature = GenericQuantityObjectSignature.getRealNumberSignature();
		signature.setRealizationName("matlab");
	}
	
	public MatlabRealNumber(RealNumber in)
	{
		super();
		try {
			mtlbObject = new MWNumericArray(in.getValue(), MWClassID.DOUBLE);
			log.info("{}={}",mtlbVarName,mtlbObject.toString());
		}
		catch(Exception e) {
			log.error("Exception:",e);
		}
		signature = GenericQuantityObjectSignature.getRealNumberSignature();
		signature.setRealizationName("matlab");
	}	
	public void setValue(double in)
	{
		try
		{	log.info("{}={}",mtlbVarName,in);
			mtlbObject.set(1,in);
		} catch (Exception e)
		{
			log.error("Exception:",e);
		}
	}

	@Override
	public Double getValue() 
	{
		if (mtlbObject!=null)
		{
			try
			{
				return (Double) mtlbObject.get(1);
			} catch (Exception e)
			{
				log.error("Exception:",e);
			}
		}
		else 
		{	
			log.warn("The object isn't defined");
		}
		return null;
	}
	
	private static final Logger log = LoggerFactory.getLogger(MatlabRealNumber.class);
}
